package com.jilefojing.service.impl;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.jilefojing.beans.pojos.Dictionary;
import com.jilefojing.beans.pojos.enhanced.EnhancedDictionary;
import com.jilefojing.context.beans.vo.UserContext;
import com.jilefojing.dao.IDictionaryDao;
import com.jilefojing.service.IDictionaryService;
import com.jilefojing.service.base.impl.BaseService;
import com.jilefojing.util.Detect;

/**
 * 
 * @author jgshun
 * 
 */
@Service
public class DictionaryService extends BaseService<Dictionary> implements IDictionaryService {
	@Autowired
	private IDictionaryDao dictionaryDao;

	@Override
	public Dictionary getByNodeName(String nodeName) {
		List<Dictionary> dictionaries = dictionaryDao.getListByNodeName(nodeName);
		if (!Detect.notEmpty(dictionaries)) {
			return null;
		} else if (dictionaries.size() > 1) {
			throw new IllegalArgumentException("[NODENAME:" + nodeName + ",配置重复]");
		}
		return dictionaries.get(0);
	}

	@Override
	public EnhancedDictionary getEnhancedDictionaryByNodeName(String nodeName, UserContext userContext) {
		Dictionary dictionary = this.getByNodeName(nodeName);
		if (dictionary == null) {
			return null;
		}
		return new EnhancedDictionary(dictionary);
	}

	@Override
	public List<EnhancedDictionary> conventDictionariesToEnhancedDictionaries(List<Dictionary> dictionaries) {
		List<EnhancedDictionary> enhancedDictionaries = new ArrayList<EnhancedDictionary>();
		for (Dictionary _Dictionary : dictionaries) {
			EnhancedDictionary _EnhancedDictionary = new EnhancedDictionary(_Dictionary);
			enhancedDictionaries.add(_EnhancedDictionary);
		}
		return enhancedDictionaries;
	}

	@Override
	public EnhancedDictionary getEnhancedDictionary(Long id, UserContext userContext) {
		List<Long> ids = new ArrayList<Long>();
		ids.add(id);
		List<EnhancedDictionary> enhancedDictionaries = this.getEnhancedDictionaries(ids, userContext);
		if (!Detect.notEmpty(enhancedDictionaries)) {
			return null;
		}
		return enhancedDictionaries.get(0);
	}

	@Override
	public List<EnhancedDictionary> getEnhancedDictionaries(List<Long> ids, UserContext userContext) {
		List<Dictionary> dictionaries = dictionaryDao.getListByIds(ids);
		if (!Detect.notEmpty(dictionaries)) {
			return null;
		}
		return this.conventDictionariesToEnhancedDictionaries(dictionaries);
	}

	@Override
	public Dictionary save(Dictionary record, UserContext userContext) {
		return this.saveSelective(record, userContext);
	}

	@Override
	public void deleteById(Object id, UserContext userContext) {
		Dictionary dictionary = dictionaryDao.getById(id);
		if (dictionary == null) {
			return;
		}
		// this.checkDelete(dictionary);
		super.deleteById(id, userContext);
	}

	@Override
	public void deleteByNodeName(String nodeName, UserContext userContext) {
		List<Dictionary> dictionaries = dictionaryDao.getListByNodeName(nodeName);
		if (!Detect.notEmpty(dictionaries)) {
			return;
		}
		// this.checkDelete(dictionaries);
		Dictionary record = new Dictionary();
		record.setNodeName(nodeName);
		super.delete(record, userContext);
	}

}
